package com.ym.soso.service.impl;

import com.ym.base.enums.FlagEnum;
import com.ym.core.shiro.ShiroUtil;
import com.ym.core.utils.tkmybatis.BaseSVImpl;
import com.ym.core.utils.tkmybatis.TK;
import com.ym.soso.dao.PharmacyInfoDao;
import com.ym.soso.domain.entity.PharmacyInfoEntity;
import com.ym.soso.domain.param.PharmacyInfoQueryParam;
import com.ym.soso.domain.param.PharmacyInfoUpdateParam;
import com.ym.soso.domain.vo.PharmacyInfoListPcVO;
import com.ym.medical.enums.CodeRuleEnum;
import com.ym.medical.service.ISysCodeRuleSV;
import com.ym.soso.domain.param.PublishParam;
import com.ym.soso.service.IPharmacyInfoSV;
import com.ym.soso.service.search.service.PharmacyInfoIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 药店信息Service业务层处理
 *
 * @author ym
 * @date 2020-09-02
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PharmacyInfoSVImpl extends BaseSVImpl<PharmacyInfoEntity, PharmacyInfoDao> implements IPharmacyInfoSV {

    @Autowired
    private PharmacyInfoIndexService pharmacyInfoIndexService;

    @Autowired
    private ISysCodeRuleSV sysCodeRuleSV;

    /**
     * 查询药店信息列表
     *
     * @param queryParam 药店信息 查询参数
     * @return 药店信息
     */
    @Override
    public List<PharmacyInfoListPcVO> selectListPC(PharmacyInfoQueryParam queryParam) {
        List<PharmacyInfoListPcVO> pharmacyInfos = mapper.selectListPC(queryParam);
        return pharmacyInfos;
    }

    /**
     * 新增药店信息
     *
     * @param updateParam 药店信息 新增参数
     * @return 结果
     */
    @Override
    public Long insertPC(PharmacyInfoUpdateParam updateParam) {
        PharmacyInfoEntity entity = updateParam.covert();
        entity.setCreSb(ShiroUtil.getLoginId(true));
        entity.setCreTime(new Date());
        entity.setTheCode(sysCodeRuleSV.createCode(CodeRuleEnum.PHARMACY));
        Long result = this.insert(entity);

        if(result > 0){
            PharmacyInfoEntity pharmacyInfoEntity = selectById(updateParam.getId());
            pharmacyInfoIndexService.saveOrUpdate(pharmacyInfoEntity);
        }
        return entity.getId();
    }

    /**
     * 修改药店信息
     *
     * @param updateParam 药店信息 修改参数
     * @return 结果
     */
    @Override
    public int updateByIdPC(PharmacyInfoUpdateParam updateParam) {
        PharmacyInfoEntity entity = updateParam.covert();
        entity.setUptSb(ShiroUtil.getLoginId(true));
        int result = this.updateById(entity);

        if (result > 0) {
            PharmacyInfoEntity pharmacyInfoEntity = selectById(updateParam.getId());
            pharmacyInfoIndexService.saveOrUpdate(pharmacyInfoEntity);
        }
        return result;
    }

    @Override
    public void delete(Long[] ids) {
        PharmacyInfoEntity pharmacyInfo = new PharmacyInfoEntity();
        pharmacyInfo.setDeleted(FlagEnum.IS.getKey());
        int result = updateByIds(pharmacyInfo, ids);

        if(result > 0){
            pharmacyInfoIndexService.deleteAndSwapHeight(ids);
        }
    }

    @Override
    public void release(PublishParam publishParams) {
        PharmacyInfoEntity pharmacyInfo = new PharmacyInfoEntity();
        pharmacyInfo.setPublishStatus(publishParams.getState());
        pharmacyInfo.setPublishTime(new Date());
        pharmacyInfo.setPublisher(ShiroUtil.getLoginId());
        Long[] ids = publishParams.getIds();
        int result = updateByParams(pharmacyInfo, TK.selectWhere(PharmacyInfoEntity.class).andIn(PharmacyInfoEntity::getId, ids).end());

        if(result > 0){
            List<PharmacyInfoEntity> pharmacyInfoEntities = selectList(TK.select(PharmacyInfoEntity.class).where().andIn(PharmacyInfoEntity::getId, ids).end());
            pharmacyInfoIndexService.saveOrUpdate(pharmacyInfoEntities);
        }
    }
}
